package hiatao.puzzle.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import haitao.puzzle.R;
import hiatao.puzzle.adapter.MessageAdapter;
import hiatao.puzzle.config.MyApplication;
import hiatao.puzzle.entity.ChatMessage;
import hiatao.puzzle.entity.User;
import hiatao.puzzle.utils.StatusBarCompat;
import hiatao.puzzle.widget.XListView;

public class SixinActivity  extends Activity{

    private XListView listview;
    private MessageAdapter adapter;
    private TextView title_tv;
    private EditText et_sendmessage;
    private Button btn_send;
    
    private User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_sixin);
		findViews();
		init();
		StatusBarCompat.compat(this, getResources().getColor(R.color.primary));
    }
	private void findViews() {
		
        listview = (XListView) findViewById(R.id.listview);
        title_tv = (TextView) findViewById(R.id.title_tv);
        btn_send = (Button) findViewById(R.id.btn_send);
        et_sendmessage = (EditText) findViewById(R.id.et_sendmessage);
	}

	private void init() {
		
		String title = getIntent().getStringExtra("TITLE");
		title_tv.setText(title);
		user = (User) getIntent().getSerializableExtra("USER");
		et_sendmessage.setHint("私信"+user.getName());
		title_tv.setText(user.getName());
		findViewById(R.id.top_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SixinActivity.this.finish();
			}
		});
		btn_send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				sendMeassage();
			}
		});
		adapter  = new MessageAdapter (this, getTestData());
		listview.setPullLoadEnable(false);
		listview.setPullRefreshEnable(false);
		listview.setAdapter(adapter);
	}
	
	protected void sendMeassage() {
		
		if(!TextUtils.isEmpty(et_sendmessage.getText())){
			ChatMessage message  = new ChatMessage();
			message.setContent(et_sendmessage.getText().toString());
			message.setSend(true);
			message.setUserHead(MyApplication.getInstance().user.getHeadUrl());
			adapter.appendMessage(message);
			et_sendmessage.setText("");
			listview.setSelection(adapter.getCount()-1);
		}
	}
	private List<ChatMessage> getTestData() {
		 List<ChatMessage> chatMessages= new ArrayList<ChatMessage>();
		 ChatMessage message = new ChatMessage();
		 message.setContent("hello");
		 message.setSend(false);
		 message.setUserHead(user.getHeadUrl());
		 chatMessages.add(message);
/*		 ChatMessage message2 = new ChatMessage();
		 message2.setComtent("hello");
		 message2.setSend(true);
		 message2.setUserHead(MyApplication.getInstance().user.getHeadUrl());
		 chatMessages.add(message2);*/
		return chatMessages;
	}

	

}
